import { defineStore } from 'pinia'
import request from '@/utils/request'

import { ApiRes, CategoryItem, TopCategory, SubCategory } from '@/types/data'
import { topCategory } from '../constants'

const defaultCategory = topCategory.map(item => {
  return {
    name: item
  }
})

export default defineStore('category', {
  state() {
    return {
      list: defaultCategory as CategoryItem[],
      topCategory: {} as TopCategory,
      subCategory: {} as SubCategory
    }
  },
  actions: {
    async getCategoryList() {
      const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
      // 给每一个一级分类添加一个open属性，用于控制显示隐藏
      res.data.result.forEach(item => item.open = false)
      this.list = res.data.result
    },
    show(id: string) {
      const category = this.list.find(item => item.id === id)
      category!.open = true
    },
    hide(id: string) {
      const category = this.list.find(item => item.id === id)
      category!.open = false
    },
    async getTopCategory(id: string) {
      // get请求拼接参数的两种方法
      // const res=await request.get(`/category?id=${id}&name=zs`)
      const res = await request.get<ApiRes<TopCategory>>('/category', {
        params: {
          id
        }
      })
      this.topCategory = res.data.result
    },
    async getSubFilter(id: string) {
      const res = await request.get(
        '/category/sub/filter',
        {
          params: { id }
        }
      )
      this.subCategory = res.data.result
    }
  }
})